﻿using GN.Pay.TradeChannel.YJPay.Response;
using GN.Pay.Utils.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace GN.Pay.TradeChannel.YJPay.Request
{
    [Serializable]
    class RefundRequest : AServiceRequest
    {
        private RefundRequest()
        {

        }

        /// <summary>
        /// 获取服务
        /// </summary>
        public override string service
        {
            get
            {
                return "fastPayTradeRefund";
            }
        }
        /// <summary>
        /// 交易号  
        /// 交易号  交易流水号	字符串(20)	是	 创建即时到账订单返...P464742078516256741A53
        /// </summary>
        public string tradeNo { get; set; }

        /// <summary>
        /// 退款金额
        /// </summary>
        public decimal refundAmount { get; set; }

        /// <summary>
        /// 退款原因 	退款原因	字符串		未收到货物
        /// </summary>
        public string refundReason { get; set; }

        /// <summary>
        /// 订单号
        /// </summary>
        public string orderNo { get; set; }



        public string version { get; set; }

        /// <summary>
        /// 协议请求方式
        /// </summary>
        public string protocol { get; set; }


        /// <summary>
        /// 退款流水号
        /// </summary>
        public string merchOrderNo { get; set; }

        /// <summary>
        /// 退款返回Url
        /// </summary>
        public string returnUrl { get; set; }

        /// <summary>
        /// 退款通知Url
        /// </summary>
        public string notifyUrl { get; set; }

        /// <summary>
        /// 创建请求
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="order"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static RefundRequest CreateRequest(ITradeChannel channel,
            IRefundApplyInfo apply,
            YJPayConfigure config)
        {
            if (apply == null)
            {
                channel.ThrowChannelValidationException("请求的 apply 为 null");
            }
            RefundRequest request = new RefundRequest()
            {
                orderNo = apply.RefundNo,
                partnerId = config.ConfigureInfo.AccountId,
                protocol = "httpPost",
                refundAmount = apply.ApplyMoney,
                refundReason = apply.RefundReason,
                version = "1.0",
                tradeNo = apply.TradeNo,
                merchOrderNo = apply.OrderNo,
                notifyUrl = config.RefundNotifyUrl
            };
            return request;
        }

        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="config"></param>
        /// <param name="apply"></param>
        /// <returns></returns>
        public RefundResponse Build(ITradeChannel channel,
            YJPayConfigure config, IRefundApplyInfo apply)
        {
            var param = this.BuildSignDictionary(channel, config);
            param = param.Clone(k =>
            {
                return k;
            }, v =>
            {
                return HttpUtility.UrlEncode(v, config.Encoding);
            });
            string url = config.GatewayUrl + "?";
            string responseString = HttpUtils.PostString(url, param, config.Encoding);
            return new RefundResponse(responseString, channel, config, apply);
        }
    }
}
